package com.ruoyi.buy.service.impl;

import com.ruoyi.buy.domain.entity.PO.BuySpaMainInfoPO;
import com.ruoyi.buy.domain.entity.PO.BuySpaPricePO;
import com.ruoyi.buy.domain.entity.buy0001.BuyDrawingImportData;
import com.ruoyi.buy.domain.entity.buy0001.BuySpa;
import com.ruoyi.buy.domain.entity.buy0001.BuySpaImportData;
import com.ruoyi.buy.domain.entity.buy0001.BuySpaSearchInDto;
import com.ruoyi.buy.mapper.Buy0001Mapper;
import com.ruoyi.buy.mapper.PO.BuySpaMainInfoMapper;
import com.ruoyi.buy.mapper.PO.BuySpaPriceMapper;
import com.ruoyi.buy.service.IBuy0001Service;
import com.ruoyi.buy.service.ICommonService;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 参数配置 服务层实现
 * 
 * @author ruoyi
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class Buy0001ServiceImpl implements IBuy0001Service
{

    @Resource
    private Buy0001Mapper buy0001Mapper;

    @Resource
    private BuySpaPriceMapper buySpaPriceMapper;

    @Resource
    private BuySpaMainInfoMapper buySpaMainInfoMapper;

    @Resource
    private ICommonService commonService;


    @Override
    public List<BuySpa> selectSpaList(BuySpaSearchInDto param) {
        List<BuySpa> result = this.buy0001Mapper.selectSpaList(param);
        return result;
    }

    @Override
    public void deleteDictDataByIds(Long[] iDs) {
        for (Long iD : iDs)
        {
            buy0001Mapper.deleteSpaDataById(iD);
        }
    }

    @Override
    public int insertSpaInfo(BuySpa param) throws Exception {
        //先查询零件是否存在
        BuySpaSearchInDto searchPara = new BuySpaSearchInDto();
        searchPara.setvSpaName(param.getvSpaName());
        searchPara.setvSpaBrand(param.getvSpaBrand());
        searchPara.setvSpaModel(param.getvSpaModel());
        List<BuySpa> list = this.buy0001Mapper.selectSpaListByAccurate(searchPara);
        if (list.size() > 0){
            throw new Exception("该零件已存在，请重新确认零件信息");
        }
        //新增主表信息
        param.setvSpa(commonService.getNo("SPA"));
        if (StringUtils.isEmpty(param.getIsLowWarning())){
            param.setIsLowWarning("N");
        }
        param.setCreateTime(new Date());
        int flag1 = this.buy0001Mapper.insertSpaData(param);

        //再新增价格表信息
        BuySpaPricePO buySpaPricePO = new BuySpaPricePO();
        buySpaPricePO.setvSpa(param.getvSpa());
        buySpaPricePO.setvKind(param.getvKind());
        buySpaPricePO.setvSpaName(param.getvSpaName());
        buySpaPricePO.setvSpaBrand(param.getvSpaBrand());
        buySpaPricePO.setvSpaModel(param.getvSpaModel());
        buySpaPricePO.setnPrice(param.getnLastPrice());
        buySpaPricePO.setStatus("0");
        buySpaPricePO.setvContractNo(StringUtils.isEmpty(param.getvContractNo())?"新建手工写入":param.getvContractNo());
        buySpaPricePO.setnQty(param.getnQty() ==null? BigDecimal.ONE:param.getnQty());
        buySpaPricePO.setnValidityDays(param.getnValidityDays() ==null? BigDecimal.ONE:param.getnValidityDays());
        buySpaPricePO.setCreateBy(param.getCreateBy());
        buySpaPricePO.setnValidityDays(param.getnValidityDays() ==null? new BigDecimal(365):param.getnValidityDays());
        buySpaPricePO.setnDays(StringUtils.isEmpty(param.getnDays())?"":param.getnDays());
        buySpaPricePO.setSupplier(StringUtils.isEmpty(param.getSupplier())?"":param.getSupplier());

        buySpaPricePO.setdContract(StringUtils.isEmpty(param.getdContract())?null:param.getdContract());
        buySpaPricePO.setvBuyNo(param.getvBuyNo());
        buySpaPricePO.setvProjectNo(param.getvProjectNo());
        buySpaPricePO.setvProjectDesc(param.getvProjectDesc());
        buySpaPricePO.setnTotalPrice(param.getnTotalPrice());
        buySpaPricePO.setvType(param.getvType());
        buySpaPricePO.setApplyBy(param.getApplyBy());

        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, buySpaPricePO.getnValidityDays().intValue());
        buySpaPricePO.setdValidity(calendar.getTime());
        buySpaPricePO.setCreateTime(new Date());
        int flag2 = this.buySpaPriceMapper.insertSpaPrice(buySpaPricePO);

        if (flag1 == 0 || flag2 ==0){
            throw new Exception("系统异常，请联系管理员");
        }
        return 1;
    }

    @Override
    public BuySpa selectSpaById(Long iD) throws Exception {
        BuySpa result = this.buy0001Mapper.selectSpaInfoById(iD);
        if(result.getiD() ==null){
            throw new Exception("未找到对应数据，请确认");
        }
        return result;
    }

    @Override
    public int updateSpaInfo(BuySpa param) throws Exception {
        //先查询零件是否存在
        BuySpaMainInfoPO searchPara = new BuySpaMainInfoPO();
        searchPara.setvSpaName(param.getvSpaName());
        searchPara.setvSpaBrand(param.getvSpaBrand());
        searchPara.setvSpaModel(param.getvSpaModel());
       BuySpaMainInfoPO spa  = this.buySpaMainInfoMapper.selectSpaInfoByCondition(searchPara);
        if (spa.getiD() == null ){
            throw new Exception("该零件已不存在，请重新确认零件信息");
        }
        //修改主表信息,只有单位和类型可以修改
        spa.setvKind(param.getvKind());
        spa.setvUnit(param.getvUnit());
        spa.setUpdateBy(param.getUpdateBy());
        spa.setIsLowQty(param.getIsLowQty());
        spa.setIsLowWarning(StringUtils.isEmpty(param.getIsLowWarning())?"N":param.getIsLowWarning());
        spa.setRemark(param.getRemark());
        spa.setUpdateTime(new Date());
        int flag1 = this.buySpaMainInfoMapper.updateSpaInfoByID(spa);

        //查子表价格,已有的作废，新建一条记录
        BuySpaPricePO buySpaPricePO = new BuySpaPricePO();
        buySpaPricePO.setvSpaName(param.getvSpaName());
        buySpaPricePO.setvSpaBrand(param.getvSpaBrand());
        buySpaPricePO.setvSpaModel(param.getvSpaModel());
        BuySpaPricePO spaPriceResult = this.buySpaPriceMapper.selectSpaPriceByCondition(buySpaPricePO);
        if (spaPriceResult.getiD() == null ){
            throw new Exception("该零件价格已不存在，请重新确认零件价格信息");
        }
        spaPriceResult.setStatus("1");
        spaPriceResult.setUpdateBy(param.getUpdateBy());
        spaPriceResult.setUpdateTime(new Date());
        int flag2 = this.buySpaPriceMapper.updateSpaPriceByID(spaPriceResult);
        //新建一条记录
        BuySpaPricePO newPrice = new BuySpaPricePO();
        BeanUtils.copyProperties(spaPriceResult,newPrice);
        newPrice.setiD(null);
        newPrice.setnPrice(param.getnLastPrice());
        newPrice.setvContractNo(StringUtils.isEmpty(param.getvContractNo())?"手工修改":param.getvContractNo());
        newPrice.setStatus("0");
        newPrice.setnQty(param.getnQty() ==null? BigDecimal.ONE:param.getnQty());
        newPrice.setnValidityDays(param.getnValidityDays() ==null? new BigDecimal(365):param.getnValidityDays());
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, newPrice.getnValidityDays().intValue());
        newPrice.setdValidity(calendar.getTime());
        newPrice.setCreateBy(param.getUpdateBy());
        newPrice.setCreateTime(new Date());

        newPrice.setdContract(StringUtils.isEmpty(param.getdContract())?null:param.getdContract());
        newPrice.setvBuyNo(param.getvBuyNo());
        newPrice.setvProjectNo(param.getvProjectNo());
        newPrice.setvProjectDesc(param.getvProjectDesc());
        newPrice.setnTotalPrice(param.getnTotalPrice());
        newPrice.setvType(param.getvType());
        newPrice.setApplyBy(param.getApplyBy());

        int flag3 = this.buySpaPriceMapper.insertSpaPrice(newPrice);
        if (flag1 == 0 || flag2 ==0|| flag3 ==0){
            throw new Exception("系统异常，请联系管理员");
        }

        return 1;
    }

    @Override
    public List<BuySpaPricePO> viewPrice(BuySpa param) {
        BuySpaPricePO para = new BuySpaPricePO();
        para.setvSpaName(param.getvSpaName());
        para.setvSpaModel(param.getvSpaModel());
        para.setvSpaBrand(param.getvSpaBrand());
        List<BuySpaPricePO> list = this.buySpaPriceMapper.selectSpaPrices(para);
        return list;
    }

    @Override
    public BuySpaPricePO selectSpaByCondition(BuySpaPricePO param) throws Exception {
        BuySpaPricePO buySpaPricePO = this.buySpaPriceMapper.selectSpaPriceByCondition(param);
        return buySpaPricePO;
    }

    @Override
    public int importSpaData(List<BuySpaImportData> list,String oprName) throws Exception {
        for (BuySpaImportData item:list
             ) {
            if(StringUtils.isEmpty(item.getVSpaName())){
                throw new Exception("存在物料名称为空的数据，请确认");
            }else if(StringUtils.isEmpty(item.getVSpaModel())){
                throw new Exception("存在规格型号为空的数据，请确认");
            }else if(StringUtils.isEmpty(item.getVSpaBrand())){
                throw new Exception("存在品牌为空的数据，请确认");
            }
            //先查询零件是否存在
            BuySpaMainInfoPO searchPara = new BuySpaMainInfoPO();
            searchPara.setvSpaName(item.getVSpaName());
            searchPara.setvSpaBrand(item.getVSpaBrand());
            searchPara.setvSpaModel(item.getVSpaModel());
            BuySpaMainInfoPO spa  = this.buySpaMainInfoMapper.selectSpaInfoByCondition(searchPara);
            BuySpa param = new BuySpa();
            com.ruoyi.common.utils.bean.BeanUtils.copyBeanProp(param,item);
            if (spa == null || spa.getiD() == null ){
                param.setCreateBy(oprName);
                param.setvKind("02");
                this.insertSpaInfo(param);
            }else{
                param.setUpdateBy(oprName);
                this.updateSpaInfo(param);
            }
        }

        return 1;
    }

    @Override
    public int importDrawData(List<BuyDrawingImportData> list, String oprName) throws Exception {
        for (BuyDrawingImportData item:list
             ) {
            if(StringUtils.isEmpty(item.getVSpaName())){
                throw new Exception("存在物料名称为空的数据，请确认");
            }else if(StringUtils.isEmpty(item.getVSpaModel())){
                throw new Exception("存在规格型号为空的数据，请确认");
            }else if(StringUtils.isEmpty(item.getVSpaBrand())){
                throw new Exception("存在品牌为空的数据，请确认");
            }

            //先查询零件是否存在
            BuySpaMainInfoPO searchPara = new BuySpaMainInfoPO();
            searchPara.setvSpaName(item.getVSpaName());
            searchPara.setvSpaBrand(item.getVSpaBrand());
            searchPara.setvSpaModel(item.getVSpaModel());
            BuySpaMainInfoPO spa  = this.buySpaMainInfoMapper.selectSpaInfoByCondition(searchPara);

            BuySpa param = new BuySpa();
            com.ruoyi.common.utils.bean.BeanUtils.copyBeanProp(param,item);
            if (spa == null || spa.getiD() == null ){
                param.setCreateBy(oprName);
                param.setvKind("01");
                this.insertSpaInfo(param);
            }else{
                param.setUpdateBy(oprName);
                this.updateSpaInfo(param);
            }
        }
        return 1;
    }
}
